using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emlyn.Applications.Scratch.Graphics.Tools;
using Microsoft.DirectX.Direct3D;

namespace Emlyn.Applications.Scratch.Graphics.Chapter7
{
    public partial class frmChapter7 : Form
    {
        public frmChapter7()
        {
            InitializeComponent();
        }

        DirectXWrapper _wrapper = null;
        ScrollingBackground _scrollingBackground = null;

        Bitmap _tilesBitmap = null;
        MapData _mapData = null;

        int ScrollX = 0;
        int ScrollY = 0;
        int SpeedX = 0;
        int SpeedY = 0;

        const int TileWidth = 64;
        const int TileHeight = 80;
        const int ScrollStep = 8;

        private void frmChapter7_Load(object sender, EventArgs e)
        {
//            _tilesBitmap = new Bitmap(@"x:\emlyn\images\pipemap.bmp");
            MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory);

            _tilesBitmap = new Bitmap(AppDomain.CurrentDomain.BaseDirectory +  @"\pipemap.bmp");

            _mapData = new MapData();

            _mapData.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\pipemap.csv");
            _mapData.QuadrupleSize();
            _mapData.QuadrupleSize();
            _mapData.QuadrupleSize();
            _mapData.QuadrupleSize();
            _mapData.QuadrupleSize();

            InitAll();

            _wrapper._device.DeviceLost += new EventHandler(DeviceLostHandler);
        }

        private void DisposeAll()
        {
            if (_scrollingBackground != null)
            {
                _scrollingBackground.Dispose();
                _scrollingBackground = null;
            }
            if (_wrapper != null)
            {
                _wrapper.Dispose();
                _wrapper = null;
            }
        }

        private void InitAll()
        {
            timer1.Enabled = false;

            try
            {
                DisposeAll();

                _wrapper = new DirectXWrapper(this.ClientSize.Width, this.ClientSize.Height, this);

                _scrollingBackground = new ScrollingBackground(_wrapper, _tilesBitmap, TileWidth, TileHeight, _mapData);
            }
            finally
            {
                timer1.Enabled = true;
            }
        }

        private void frmChapter7_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Enabled = false;
            DisposeAll();
        }

        private void DeviceLostHandler(object sender, EventArgs e)
        {
            InitAll();
        }

        private void frmChapter7_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
            else if (e.KeyCode == Keys.Right)
                SpeedX = ScrollStep;
            else if (e.KeyCode == Keys.Left)
                SpeedX = -1 * ScrollStep;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            UpdateScrollPosition();

//            BuildTileBuffer();
            _scrollingBackground.BuildTileBuffer(ScrollX, ScrollY, _wrapper._backbuffer.Description.Width, _wrapper._backbuffer.Description.Height);

            _scrollingBackground.WriteBackgroundOntoBackBuffer(ScrollX, ScrollY);

            _wrapper.Present();
        }

        private int GameWorldWidth()
        {
            return _mapData.Width * TileWidth;
        }

        private int GameWorldHeight()
        {
            return _mapData.Height * TileHeight;
        }

        private void UpdateScrollPosition()
        {
            ScrollX += SpeedX;
            if (ScrollX < 0)
            {
                ScrollX = 0;
                SpeedX = 0;
            }
            else if (ScrollX > GameWorldWidth() - _wrapper._backbuffer.Description.Width - 1)
            {
                ScrollX = GameWorldWidth() - _wrapper._backbuffer.Description.Width - 1 ;
                SpeedX = 0;
            }

            ScrollY += SpeedY;
            if (ScrollY < 0)
            {
                ScrollY = 0;
                SpeedY = 0;
            }
            else if (ScrollY > GameWorldHeight() - _wrapper._backbuffer.Description.Height - 1)
            {
                ScrollY = GameWorldHeight() - _wrapper._backbuffer.Description.Height - 1;
                SpeedY = 0;
            }
        }

        private void frmChapter7_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X < _wrapper._backbuffer.Description.Width / 3)
                SpeedX = -1 * ScrollStep;
            else if (e.X > 2 * _wrapper._backbuffer.Description.Width / 3)
                SpeedX = ScrollStep;
            else
                SpeedX = 0;

            if (e.Y < _wrapper._backbuffer.Description.Height / 3)
                SpeedY = -1 * ScrollStep;
            else if (e.Y > 2 * _wrapper._backbuffer.Description.Height / 3)
                SpeedY = ScrollStep;
            else
                SpeedY = 0;
        }


    }
}